package upay.client;

import android.app.Activity;
import android.app.Dialog;
import android.content.IntentSender;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.common.SignInButton;
import com.google.android.gms.plus.PlusClient;

public class MainActivity extends Activity implements OnClickListener,
	PlusClient.ConnectionCallbacks,
	PlusClient.OnConnectionFailedListener,
    PlusClient.OnAccessRevokedListener {
	
	/* ---- Constants ---- */

	private static final int DIALOG_GET_GOOGLE_PLAY_SERVICES = 1;
	private static final int REQUEST_CODE_SIGN_IN = 1;
	private static final int REQUEST_CODE_GET_GOOGLE_PLAY_SERVICES = 2;
	private static final int GPS_ERRORDIALOG_REQUEST = 9001;
	
	/* ---- Instance Variables ---- */

	private TextView mSignInStatus;
	private PlusClient mPlusClient;
	private SignInButton mSignInButton;
	private ConnectionResult mConnectionResult;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        mPlusClient = new PlusClient.Builder(this, this, this)
        	.setActions(MomentUtil.ACTIONS)
        	.build();

        mSignInStatus = (TextView) findViewById(R.id.sign_in_status);
        mSignInButton = (SignInButton) findViewById(R.id.sign_in_button);
        mSignInButton.setOnClickListener(this);

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }


	@Override
	public void onClick(View view) {
        switch(view.getId()) {
        case R.id.sign_in_button:
            int available = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
            if (available != ConnectionResult.SUCCESS) {
            	if (GooglePlayServicesUtil.isUserRecoverableError(available)) {
                    Dialog dialog = GooglePlayServicesUtil.getErrorDialog(available, this, GPS_ERRORDIALOG_REQUEST);
                    dialog.show();
                } else {
                    Toast.makeText(this, "Connect Connect to Maps", Toast.LENGTH_SHORT).show();
                }
            }

            try {
                mSignInStatus.setText(getString(R.id.sign_in_status));
                mConnectionResult.startResolutionForResult(this, REQUEST_CODE_SIGN_IN);
            } catch (IntentSender.SendIntentException e) {
                // Fetch a new result to start.
                mPlusClient.connect();
            }
            break;
        }
	}
        
    @Override
    public void onStart() {
        super.onStart();
        mPlusClient.connect();
    }

    @Override
    public void onStop() {
        mPlusClient.disconnect();
        super.onStop();
    }


	@Override
	public void onAccessRevoked(ConnectionResult status) {
        if (status.isSuccess()) {
            mSignInStatus.setText(R.string.revoke_access_status);
        } else {
            mSignInStatus.setText(R.string.revoke_access_error_status);
            mPlusClient.disconnect();
        }
        mPlusClient.connect();
	}


	@Override
	public void onConnectionFailed(ConnectionResult result) {
        mConnectionResult = result;
        updateButtons(Boolean.FALSE);	
	}


	@Override
	public void onConnected(Bundle arg0) {
        String currentPersonName = mPlusClient.getCurrentPerson() != null
            ? mPlusClient.getCurrentPerson().getDisplayName()
            : getString(R.string.unknown_person);
        mSignInStatus.setText(getString(R.id.sign_in_status, currentPersonName));
        updateButtons(Boolean.TRUE);
	}

	@Override
	public void onDisconnected() {
        mSignInStatus.setText(R.string.loading_status);
        mPlusClient.connect();
        updateButtons(Boolean.FALSE);
	}
	
    private void updateButtons(boolean isSignedIn) {
        if (isSignedIn) {
            mSignInButton.setVisibility(View.INVISIBLE);
        } else {
            if (mConnectionResult == null) {
                // Disable the sign-in button until onConnectionFailed is called with result.
                mSignInButton.setVisibility(View.INVISIBLE);
                mSignInStatus.setText(getString(R.string.loading_status));
            } else {
                // Enable the sign-in button since a connection result is available.
                mSignInButton.setVisibility(View.VISIBLE);
                mSignInStatus.setText(getString(R.string.signed_out_status));
            }
        }
    }
    
}
